/**
 * @Title: ExampleFile.java
 * @Package com.example.package
 * @Description: Placeholder (briefly describe the purpose of this file)
 * @Author: Evan
 * @Date: 2023-08-18
 * @Version: V1.0
 *
 * Copyright Notice:
 * This project is released in open source and follows the applicable open source license.
 * All rights reserved. (c) 2023 Evan.
 * This project is protected by copyright laws and international copyright treaties.
 * Unauthorized reproduction, sale, copy, modification, merge, publication, distribution,
 * licensing, and/or sales of this software and its copies are prohibited without permission.
 * Please contact the author for authorization.
 * Contact Information:
 * @Email: uyevan@163.com
 */

package com.evan.biznak.vip;

import static com.evan.biznak.vip.Util.ShaUtil.isOrgApp;

import android.app.Application;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Process;
import android.util.Log;

import com.evan.biznak.vip.Util.FileUtil;
import com.evan.biznak.vip.Util.WindowUtils;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.cookie.CookieManger;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class AppLocation extends Application {
    private Context context;
    private Thread.UncaughtExceptionHandler uncaughtExceptionHandler;

    public Context getContext() {
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        /*闪退报告*/
        this.uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler((thread, ex) -> {
            String data = String.valueOf(new Date().getTime());
            FileUtil.writeFile(FileUtil.getPackageDataDir(getContext()).concat("/Biznak/程序崩溃报告/".concat(data).concat(".txt")), getStackTrace(ex));
            // 放入剪切板
            copyStr("程序闪退报告已存到：[应用私目录/Biznak/程序崩溃报告]里，请发到开发者QQ：3487297072");
            uncaughtExceptionHandler.uncaughtException(thread, ex);
        });

        context = getApplicationContext();
        /*检测*/
        checkSign();
        /*EasyHttp 初始化*/
        EasyHttp.init(this);
        EasyHttp.getInstance().setCookieStore(new CookieManger(this));
    }

    private String getStackTrace(Throwable th) {
        final Writer result = new StringWriter();

        final PrintWriter printWriter = new PrintWriter(result);
        Throwable cause = th;

        while (cause != null) {
            cause.printStackTrace(printWriter);
            cause = cause.getCause();
        }
        final String stacktraceAsString = result.toString();
        printWriter.close();
        return stacktraceAsString;
    }

    private boolean copyStr(String copyStr) {
        try {
            //获取剪贴板管理器
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", copyStr);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    private void checkSign() {
        boolean is_org_app = false;
        try {
            is_org_app = isOrgApp(context);
        } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        if (!is_org_app) {
            /*如果签名不一致，说明程序被修改了，直接退出*/
            Process.killProcess((Process.myPid()));
        }
    }
}
